package com.atguigu.lease.web.app.service.impl;

import com.atguigu.lease.model.entity.ApartmentInfo;
import com.atguigu.lease.model.entity.FacilityInfo;
import com.atguigu.lease.model.entity.GraphInfo;
import com.atguigu.lease.model.entity.LabelInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.app.mapper.*;
import com.atguigu.lease.web.app.service.ApartmentInfoService;
import com.atguigu.lease.web.app.vo.apartment.ApartmentDetailVo;
import com.atguigu.lease.web.app.vo.apartment.ApartmentItemVo;
import com.atguigu.lease.web.app.vo.graph.GraphVo;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.swagger.v3.oas.annotations.media.Schema;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.List;

/**
 * @author liubo
 * @description 针对表【apartment_info(公寓信息表)】的数据库操作Service实现
 * @createDate 2023-07-26 11:12:39
 */
@Service
public class ApartmentInfoServiceImpl extends ServiceImpl<ApartmentInfoMapper, ApartmentInfo>
        implements ApartmentInfoService {

    @Autowired
    private LabelInfoMapper labelInfoMapper;
    @Autowired
    private GraphInfoMapper graphInfoMapper;
    @Autowired
    private RoomInfoMapper roomInfoMapper;
    @Autowired
    private FacilityInfoMapper facilityInfoMapper;


    @Override
    public ApartmentItemVo selectApartmentById(Long apartmentId) {
        ApartmentItemVo apartmentItemVo = new ApartmentItemVo();
        //公寓基本信息
        ApartmentInfo apartmentInfo = this.getById(apartmentId);
        //公寓标签
        List<LabelInfo> labelInfoList=labelInfoMapper.selectListByApartmentId(apartmentId);
        //公寓图片
        List<GraphInfo> graphVoList=graphInfoMapper.selectListByItemIdAndItemType(apartmentId,ItemType.APARTMENT);
        //公寓最小租金（最便宜的房间的租金）因为公寓没有租金信息
        BigDecimal minRent=roomInfoMapper.selectMinRentByApartmentId(apartmentId);

        BeanUtils.copyProperties(apartmentInfo,apartmentItemVo);
        apartmentItemVo.setGraphVoList(graphVoList);
        apartmentItemVo.setLabelInfoList(labelInfoList);
        apartmentItemVo.setMinRent(minRent);


        return apartmentItemVo;
    }

    @Override
    public ApartmentDetailVo getDetailById(Long id) {
        ApartmentDetailVo apartmentDetailVo = new ApartmentDetailVo();
        //查询公寓信息
        ApartmentInfo apartmentInfo = this.getById(id);
        //公寓图片信息
        List<GraphInfo> graphVoList=graphInfoMapper.selectListByItemIdAndItemType(id,ItemType.APARTMENT);
        //公寓的标签
        List<LabelInfo> labelInfoList=labelInfoMapper.selectListByApartmentId(id);
        //公寓的配套信息
        List<FacilityInfo> facilityInfoList=facilityInfoMapper.selectListByApartmentId(id);
        //公寓中房间的最小租金
        BigDecimal minRent=roomInfoMapper.selectMinRentByApartmentId(id);
        //公寓是否被删除
        Boolean isDelete=apartmentInfo.getIsDeleted()==1;

        BeanUtils.copyProperties(apartmentInfo,apartmentDetailVo);

        apartmentDetailVo.setIsDelete(isDelete);
        apartmentDetailVo.setMinRent(minRent);
        apartmentDetailVo.setFacilityInfoList(facilityInfoList);
        apartmentDetailVo.setLabelInfoList(labelInfoList);
        apartmentDetailVo.setGraphVoList(graphVoList);



        return apartmentDetailVo;
    }
}




